<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="560px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="70px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="form.nickname" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色" prop="r_id">
            <el-select v-model="form.r_id" size="small" clearable>
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-radio v-model="form.sex" :label="1" checked>男</el-radio>
            <el-radio v-model="form.sex" :label="2">女</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="state">
            <el-select v-model="form.state" size="small" clearable>
              <el-option
                v-for="item in states"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { queryRoles } from '@/api/role'
import { addTo, updateTo } from '@/api/user'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      roles: [],
      rules: {
        r_id: [{ required: true, trigger: 'blur', message: '角色不能为空' }],
        email: [{ required: true, trigger: 'blur', message: '邮箱不可为空' }],
        role: [{ required: true, trigger: 'blur', message: '角色不可为空' }],
        state: [{ required: true, trigger: 'blur', message: '状态不可为空' }],
        nickname: [{ required: true, trigger: 'blur', message: '昵称不可为空' }],
        phone: [{ required: true, trigger: 'blur', message: '手机号不可为空' }],
        sex: [{ required: true, trigger: 'blur', message: '性别不可为空' }]
      },
      states: [{ 'value': 0, 'label': '禁用' }, { 'value': 1, 'label': '启用' }],
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        this.queryRoles()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    queryRoles() {
      queryRoles().then(response => {
        this.roles = response.role
      }).catch(error => {
        this.$message.error(error.msg ? error.msg : '操作失败')
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          try {
            this.$set(this.form, 'role', this.form['r_id'])
            if (this.info.type === 'add') {
              addTo(this.form).then(response => {
                this.getData()
                this.handleCancel()
                this.$message.success('添加成功')
              })
            }
            if (this.info.type === 'edit') {
              updateTo(this.form).then(response => {
                this.getData()
                this.handleCancel()
                this.$message.success('编辑成功')
              })
            }
          } catch (err) {
            this.$message.error(err.msg)
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
